﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SKDB_Matcher
{
    public partial class CAlgorithmWeightDlg : Form
    {
        private List<CWeightControlItem> m_WeightItems;

        private static Boolean m_AutoWeight = true;

        public CAlgorithmWeightDlg()
        {            
            InitializeComponent();

            m_WeightItems = new List<CWeightControlItem>();

            foreach (CAbstractAlgorithm Algo in CMatcher.Algorithms)
            {
                CWeightControlItem weightItem = new CWeightControlItem(Algo);
                weightItem.RaiseCustomEvent += WeightsChanged;
                weightItem.Parent = groupBox1;
                weightItem.Location = new Point(6, 19 + m_WeightItems.Count * weightItem.Height);
                m_WeightItems.Add(weightItem);
            }
            checkBox1.Checked = m_AutoWeight;
        }

        public void WeightsChanged(object sender, EventArgs e)
        {
            if(checkBox1.Checked)
                CalcWeights();
        }

        private void CalcWeights(int Static = -1)
        {
            float Sum = 0.0f;
            for (int i = 0; i < m_WeightItems.Count; i++)
            {
                if(i != Static)
                    Sum += m_WeightItems[i].Weight;
            }
            if(Sum >= 1f)
                for (int i = 0; i < m_WeightItems.Count; i++)
                {
                    if (i != Static)
                        m_WeightItems[i].Weight = m_WeightItems[i].Weight / Sum;
                }
        }

        private void btOk_Click(object sender, EventArgs e)
        {
            
            float Sum = 0.0f;
            for (int i = 0; i < m_WeightItems.Count; i++)
                Sum += m_WeightItems[i].Weight;
            if (Math.Abs(Sum - 1f) > 0.05f)
            {
                if (MessageBox.Show("Die Summe aller Gewichte sollte 100% betragen, soll dies nun automatisch berichtigt werden?", "Warnung", MessageBoxButtons.YesNo, MessageBoxIcon.Hand) == System.Windows.Forms.DialogResult.Yes)
                {
                    for (int i = 0; i < m_WeightItems.Count; i++)
                    {
                        m_WeightItems[i].Weight = m_WeightItems[i].Weight / Sum;
                    }
                }
            }
            DialogResult = System.Windows.Forms.DialogResult.OK;
            foreach (CWeightControlItem Item in m_WeightItems)
                Item.Save();
            Close();
        }

        private void btReset_Click(object sender, EventArgs e)
        {
            foreach (CWeightControlItem Item in m_WeightItems)
                Item.Reset();
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            m_AutoWeight = checkBox1.Checked;
        }
    }
}
